package com.yypx.action;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.yypx.common.Constants;
import com.yypx.common.utils.StringUtil;
import com.yypx.entity.PageBean;
import com.yypx.entity.Student;
import com.yypx.entity.StudentType;
import com.yypx.entity.TypeValueObject;
import com.yypx.service.CertificateTypeService;
import com.yypx.service.StudentService;
import com.yypx.service.StudentTypeService;

/**
 * @author roman
 * 
 */
@Scope("prototype")
@Component("studentAction")
public class StudentAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private StudentService studentService;
	@Autowired
	private StudentTypeService studentTypeService;
	@Autowired
	private CertificateTypeService certificateTypeService;
	private List<TypeValueObject> tlist;
	private List<Map<String, Object>> resultList;
	private List<StudentType> statusList;
	private PageBean pageBean;
	private static final String PARAM_PAGE = "page";
	private static final String PARAM_PAGESIZE = "pagesize";
	private Map<String, Object> rMap;

	/**
	 * 新增
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		tlist = certificateTypeService.listAll();
		statusList = studentTypeService.selAll();
		return "add";
	}

	/**
	 * 新增保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addSave() throws Exception {
		Student student = new Student();
		student.setSname(getRequest().getParameter("sname"));
		student.setGender(Integer.valueOf(getRequest().getParameter("gender")));
		student.setScode(getRequest().getParameter("scode"));
		// 初始密码为学号
		student.setPas(student.getScode());
		student.setStudentphone(getRequest().getParameter("sphone"));
		student.setParphone(getRequest().getParameter("pphone"));
		student.setEmail(getRequest().getParameter("email"));
		student.setOtherconnect(getRequest().getParameter("otherconnect"));
		student.setDescribe(getRequest().getParameter("describe"));
		student.setWherecome(getRequest().getParameter("wherecome"));
		student.setLesson(getRequest().getParameter("lesson"));
		student.setCertificate(Integer.valueOf(getRequest().getParameter("certtype")));
		student.setCertno(getRequest().getParameter("certno"));
		student.setStatus(Integer.valueOf(getRequest().getParameter("studentstatus")));
		if (StringUtil.isStringHasContent(getRequest().getParameter("birthday"))) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				student.setBirthday(sdf.parse(getRequest().getParameter("birthday")));
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		student.setCreatedby(UUID.randomUUID());
		student.setSid(UUID.randomUUID());
		studentService.add(student);
		return "addsuccess";
	}

	/**
	 * 列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		Map map = new HashMap<String, Object>();
		int totalCount = studentService.selCountByCondition(map);
		setPageBean(new PageBean(totalCount, getRequest().getParameter(PARAM_PAGESIZE), getRequest().getParameter(PARAM_PAGE), getRequest().getRequestURI()));
		map.put(Constants.SQL_MAP_LIMIT, pageBean.getPageSize());
		map.put(Constants.SQL_MAP_OFFSET, pageBean.getStartResult());
		List<Student> list = studentService.selAllByCondition(map);
		resultList = studentService.getResultList(list);
		return "list";
	}

	/**
	 * 浏览
	 * 
	 * @return
	 * @throws Exception
	 */
	public String view() throws Exception {
		UUID sid = null;
		if (StringUtil.isStringHasContent(getRequest().getParameter("sid"))) {
			sid = UUID.fromString(getRequest().getParameter("sid"));
			Student student = studentService.findById(sid);
			if (student != null) {
				setrMap(studentService.getResultMap(student));
				getRequest().setAttribute("rMap", rMap);
			}
		}
		return "view";
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		UUID sid = null;
		if (StringUtil.isStringHasContent(getRequest().getParameter("sid"))) {
			sid = UUID.fromString(getRequest().getParameter("sid"));
			Student student = studentService.findById(sid);
			if (student != null) {
				setrMap(studentService.getResultMap(student));
				getRequest().setAttribute("rMap", rMap);
			}
		}
		tlist = certificateTypeService.listAll();
		statusList = studentTypeService.selAll();
		return "edit";
	}

	/**
	 * 编辑保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String editSave() throws Exception {
		Student student = new Student();
		UUID sid = null;
		if (StringUtil.isStringHasContent(getRequest().getParameter("sid"))) {
			sid = UUID.fromString(getRequest().getParameter("sid"));
			student = studentService.findById(sid);
		}
		student.setSname(getRequest().getParameter("sname"));
		student.setGender(Integer.valueOf(getRequest().getParameter("gender")));
		student.setScode(getRequest().getParameter("scode"));
		student.setStudentphone(getRequest().getParameter("sphone"));
		student.setParphone(getRequest().getParameter("pphone"));
		student.setEmail(getRequest().getParameter("email"));
		student.setOtherconnect(getRequest().getParameter("otherconnect"));
		student.setDescribe(getRequest().getParameter("describe"));
		student.setWherecome(getRequest().getParameter("wherecome"));
		student.setLesson(getRequest().getParameter("lesson"));
		student.setCertificate(Integer.valueOf(getRequest().getParameter("certtype")));
		student.setCertno(getRequest().getParameter("certno"));
		student.setStatus(Integer.valueOf(getRequest().getParameter("studentstatus")));
		if (StringUtil.isStringHasContent(getRequest().getParameter("birthday"))) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				student.setBirthday(sdf.parse(getRequest().getParameter("birthday")));
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		student.setLastupdateby(UUID.randomUUID());
		studentService.update(student);
		return "editsuccess";
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		UUID sid = null;
		if (StringUtil.isStringHasContent(getRequest().getParameter("sid"))) {
			sid = UUID.fromString(getRequest().getParameter("sid"));
			try {
				studentService.delById(sid);
			} catch (Exception e) {
				getRequest().setAttribute("err", e.getMessage());
				return "error";
			}
		}
		return "delete";
	}

	public void setResultList(List<Map<String, Object>> resultList) {
		this.resultList = resultList;
	}

	public List<Map<String, Object>> getResultList() {
		return resultList;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setStatusList(List<StudentType> statusList) {
		this.statusList = statusList;
	}

	public List<StudentType> getStatusList() {
		return statusList;
	}

	public void setTlist(List<TypeValueObject> tlist) {
		this.tlist = tlist;
	}

	public List<TypeValueObject> getTlist() {
		return tlist;
	}

	public void setrMap(Map<String, Object> rMap) {
		this.rMap = rMap;
	}

	public Map<String, Object> getrMap() {
		return rMap;
	}
}
